import Vue from "vue";
import {addOrderData,wechatUnifiedOrderData,getPayOrderData,getOrderDetailsData,applyRefundOrderData,getRefundOrderData} from "../../api/order";
export default{
    namespaced:true,
    state:{
        payOrders:[],//已付款订单的数据
        orderDetails:{},//订单详情数据
        refundOrders:[],//退款订单的数据
    },
    mutations:{
        //设置已付款订单的数据
        ["SET_PAY_ORDERS"](state,payload){
            state.payOrders=payload.payOrders;
        },
        //设置已付款订单的分页数据
        ["SET_PAY_ORDERS_PAGE"](state,payload){
            state.payOrders.push(...payload.payOrders);
        },
        //设置订单详情
        ["SET_ORDER_DETAILS"](state,payload){
            state.orderDetails=payload.orderDetails;
        },
        //改变订单状态
        ["SET_ORDER_STATE"](state,payload){
            if(state.orderDetails.order_desc){
                if(payload.is_meal=='1'){//套餐
                    level1:
                    for(let i=0;i<state.orderDetails.order_desc.length;i++){
                        level2:
                        for(let j=0;j<state.orderDetails.order_desc[i].omi_data.length;j++){
                            if(state.orderDetails.order_desc[i].omi_data[j].order_item_id==payload.order_item_id){
                                state.orderDetails.order_desc[i].omi_data[j].refund_state='-1';
                                Vue.set(state.orderDetails.order_desc[i].omi_data,j,state.orderDetails.order_desc[i].omi_data[j]);
                                break level1;
                            }
                        }
                    }
                }else{//非套餐
                    for(let i=0;i<state.orderDetails.order_desc.length;i++){
                        if(state.orderDetails.order_desc[i].order_item_id==payload.order_item_id){
                            state.orderDetails.order_desc[i].refund_state='-1';
                            Vue.set(state.orderDetails.order_desc,i,state.orderDetails.order_desc[i]);
                            break;
                        }
                    }
                }
            }
        },
        //设置退款订单数据
        ["SET_REFUND_ORDERS"](state,payload){
            state.refundOrders=payload.refundOrders;
        },
        //设置退款订单分页数据
        ["SET_REFUND_ORDERS_PAGE"](state,payload){
            state.refundOrders.push(...payload.refundOrders);
        }
    },
    actions:{
        //提交订单
        addOrder(conText,payload){
            addOrderData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                if(payload.complete){
                    payload.complete(res);
                }
            })
        },
        //微信小程序统一下单接口
        wechatUnifiedOrder(conText,payload){
            return wechatUnifiedOrderData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                return res;
            })
        },
        //获取已付款订单的数据
        getPayOrder(conText,payload){
            getPayOrderData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                if(res.code==200){
                    conText.commit("SET_PAY_ORDERS",{payOrders:res.data});
                    if(payload.success){
                        payload.success(res.pageinfo.pagenum);
                    }
                }else{
                    conText.commit("SET_PAY_ORDERS",{payOrders:[]});
                }
                if(payload.complete){
                    payload.complete()
                }
            })
        },
        //获取已付款订单的分页数据
        getPayOrderPage(conText,payload){
            getPayOrderData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                if(res.code==200){
                    conText.commit("SET_PAY_ORDERS_PAGE",{payOrders:res.data});
                }
            })
        },
        //获取订单详情数据
        getOrderDetails(conText,payload){
            getOrderDetailsData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                if(res.code==200){
                    conText.commit("SET_ORDER_DETAILS",{orderDetails:res.data});
                }
                if(payload.complete){
                    payload.complete();
                }
            })
        },
        //申请退款
        applyRefundOrder(conText,payload){
            applyRefundOrderData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                if(payload.complete){
                    payload.complete(res);
                }
                if(res.code==200){
                    //改变订单状态
                    conText.commit("SET_ORDER_STATE",{order_item_id:payload.order_item_id,is_meal:payload.is_meal})
                }
            })
        },
        //查看退款订单数据
        getRefundOrder(conText,payload){
            getRefundOrderData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                if(res.code==200){
                    conText.commit("SET_REFUND_ORDERS",{refundOrders:res.data});
                    if(payload.success){
                        payload.success(res.pageinfo.pagenum);
                    }
                }else{
                    conText.commit("SET_REFUND_ORDERS",{refundOrders:[]});
                }
                if(payload.complete){
                    payload.complete();
                }
            })
        },
        //查看退款订单分页数据
        getRefundOrderPage(conText,payload){
            getRefundOrderData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                if(res.code==200){
                    conText.commit("SET_REFUND_ORDERS_PAGE",{refundOrders:res.data});
                }
            })
        }
    }
}